# -*- coding: utf-8 -*-
# desc:同步用例到自动化平台
import json

import numpy as np
import requests

BASE_URL = "http://localhost:8080/api/v1"


class Platform():
    def __init__(self):
        self.url = BASE_URL
        self.api_token = "97dda6a7bac3f0126bde8ba897e00ec6"
        self.headers = {
            "api_token": self.api_token
        }

    def sync_fio(self, param):
        """同步用例到平台"""
        res = requests.post(self.url + "/fio", headers=self.headers, json=param).text
        print(res)

    def sync(self):
        filepath = "E:\workspace\gowork\src\palmer\server\scripts\fio_result.txt"
        with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
            data = f.read()
            for i in data.split("\n"):
                i = json.loads(i)
                param = {
                    'iodepth': i['iodepth'],
                    'ioengine': i['ioengine'],
                    'wiops': np.random.random(1000),
                    'riops': np.random.random(1000),
                }
                self.sync_fio(param)

    def sync1(self):
        for i in range(1000):
            param = {
                'iodepth': 1,
                'ioengine': "windows",
                'wiops': str(np.random.randint(1,1000)),
                'riops': str(np.random.randint(1,1000)),
                'wbw': str(np.random.randint(1,1000)),
                'rbw': str(np.random.randint(1,1000)),
                'wlazy': str(np.random.randint(1,1000)),
                'rlazy': str(np.random.randint(1,1000)),
                'numjobs': np.random.randint(1,128),
                'sync':1,
                'direct':1,
                "bs":'4K',
                'size':"",
                'filesize':"15M",
                'job_name':"4K_15M",
                'client_num':1
            }
            self.sync_fio(param)


if __name__ == "__main__":
    plat = Platform()
    plat.sync1()
